<?php

// 获取产品名称
function get_proname($pid) {
	if (empty($pid)) return;

	$arr = __query("select title from {tabpre}product where id=".$pid);
	if (! empty($arr[0]['title'])) {
		return $arr[0]['title'];
	}
}

// 获取产品价格
function get_proprice($pid) {
	if (empty($pid)) return;

	$arr = __query("select price from {tabpre}product where id=".$pid);
	if (! empty($arr[0]['price'])) {
		return $arr[0]['price'];
	}
}

// 获取左侧分类
function get_mian_left() {

	$cid = intval($_GET['cid']);

	$arr = __query("select * from {tabpre}class where SystemID=108 and partid=0 order by sequence asc,id asc");
	if (! empty($arr)) {
		foreach ($arr as $k=>$v) {


			$arr2 = __query("select * from {tabpre}class where SystemID=108 and partid=".$v['ID']." order by sequence asc,id asc");
			if (! empty($arr2)) {
				$str .= '<li >  
				<div class="exam_title  clearfix" >
				<p>'.$v['ClassName'].'</p>
				</div>
				<div class="exam_main" style="display:block;">
				<ul>
				<li class="clearfix">';

			} else {

				if ($cid == $v['ID']) {
					$style_cur = ' class="cur"';
				} else {
					$style_cur = '';
				}

				$str .= '<li>  
				<div class="exam_title  clearfix" >
				<p><a href="/app/index.php/product/index-cid-'.$v['ID'].'.html" '.$style_cur.'>'.$v['ClassName'].'</a></p>
				</div>';
			}

			if (! empty($arr2)) {
				foreach ($arr2 as $kk => $vv) {

					if ($cid == $vv['ID']) {
						$style2_cur = ' class="cur"';
					} else {
						$style2_cur = '';
					}
					$str .= '<a href="/app/index.php/product/index-cid-'.$vv['ID'].'.html" '.$style2_cur.'>'.$vv['ClassName'].'</a>';	
				}
			}

			if (! empty($arr2)) {
				$str .= '</li></ul></div></li>';
			} else {
				$str .= '</li>';
			}

		}
	}

	return $str;
}


function __safe($key) {
	$value = mysql_real_escape_string($_POST[$key]);
	return $value;
}

/**
 * 获取广告
 */
function getAds($id, $type=0) {
	if (empty($id)) return;

	$arr  = __query("select * from {tabpre}ads where classid=$id limit 0,1");
	if (! empty($arr)) {

		if ($type==1) {
			// 输出地址
			$str = '<a href="'. $arr[0]['content'] .'" target="_blank"><img src="'. get_urlimg($arr[0]['pic']) .'"></a>';

		} else {
			// 输出图片
			$str = "" . get_urlimg($arr[0]['pic']);

		}
		return $str;
	}
}

//去掉内容中的多余HTML代码
function replace_content($str){
	$search = array (
	"'<script[^>]*?>.*?</script>'si", // 去掉 javascript 
	"'<style[^>]*?>.*?</style>'si", // 去掉 css 
	"'<[/!]*?[^<>]*?>'si", // 去掉 HTML 标记 
	"'<!--[/!]*?[^<>]*?>'si", // 去掉 注释标记 
	"'([rn])[s]+'", // 去掉空白字符 
	"'&(quot|#34);'i", // 替换 HTML 实体 
	"'&(amp|#38);'i", 
	"'&(lt|#60);'i", 
	"'&(gt|#62);'i", 
	"'&(nbsp|#160);'i", 
	"'&(iexcl|#161);'i", 
	"'&(cent|#162);'i", 
	"'&(pound|#163);'i", 
	"'&(copy|#169);'i", 
	"'&#(d+);'e"); // 作为 PHP 代码运行 
	$replace = array ("","","","","\1","\"","&","<",">"," ",chr(161),chr(162),chr(163),chr(169),"chr(\1)"); 
	//$document为需要处理字符串，如果来源为文件可以$document = file_get_contents('http://www.sina.com.cn'); 
	$out = preg_replace($search, $replace, $str); 
	return $out;
	//echo $out; 
}
//执行SQL语句
function __query($sql) {
if (! empty($sql))
{
	global $config;
	$cModel = commonMod::initModel( $config );
	$sql = str_replace('{tabpre}', $cModel->pre, $sql);
	$arr = $cModel->query($sql);
	if (empty($arr)) $arr=array();
	return $arr;
}
}

//组合图片地址
function get_urlimg($img){
	if (!empty($img)) {
		return '/app/upload/image/'.substr($img,0,7).'/'.$img;
	}
	else{
		return 'javascript:void(0);';
	}
}
